package level4.sort
{
	import level4.level4;

	public class Insertion
	{
		public function Insertion()
		{
		}
		
		level4 static function sort(arr:Array):void{
			for(var j:int = 1; j<arr.length ;j++){
				var key:int = arr[j];
				
				var i:int = j-1;
				while(i >= 0 && arr[i] > key){
					arr[i+1] = arr[i];
					i -= 1;
				}
				arr[i+1] = key;
			}
		}
		/**
		 * 插入排序,
		 * @param arr,
		 * @param fun:Function
		 * 格式为: 输出,<0,=0,>0
		 * function(curObj:Object,keyObj:Object):int{
				return curObj.a - keyObj.a;
			}
		 * */
		level4 static function sortBy(arr:Vector,fun:Function):void{
			for(var j:int = 1;j<arr.length;j++){
				var key:Object = arr[j];
				
				var i:int = j-1;
				while(i >= 0 && fun(arr[i],key) > 0){
					arr[i+1] = arr[i];
					i -= 1;
				}
				arr[i+1] = key;
			}
		}
	}
}